package com.blindBox.back.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blindBox.back.commonResult.MsgBackToFront;
import com.blindBox.back.domain.Paper;
import com.blindBox.back.domain.Paperchosebywho;
import com.blindBox.back.mapper.PaperMapper;
import com.blindBox.back.parse.PaperParse;
import com.blindBox.back.service.PaperService;
import com.blindBox.back.service.PaperchosebywhoService;
import com.blindBox.back.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Transactional
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements PaperService {

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PaperParse paperParse;
    @Autowired
    private FileUtils fileManager;
    @Autowired
    private PaperchosebywhoService paperchosebywhoService;

    //查询出所有纸条的id集合
    @Override
    public List<Integer> getPaperIdsByList()
    {
        return paperMapper.getPaperIdsByList();
    }

    //清空所有纸条的状态
    @Override
    public void emptyAllPaperStatus() {
        paperMapper.emptyAllPaperStatus();
    }

    //总纸条数量
    @Override
    public Integer getPaperNums() {
        return paperMapper.getPaperNums();
    }

    //可用纸条数量
    @Override
    public Integer getAblePaperNums() {
        return paperMapper.getAblePaperNums();
    }

    //获取所有纸条完整信息
    @Override
    public List<Map<Object, Object>> getPapersInfo()
    {
        List<Map<Object, Object>> papersInfo = paperMapper.getPapersInfo();
        //纸条内容解密--为了照顾没有被加密过的纸条
        try
        {
            List<Map<Object, Object>> afterParse=new ArrayList<>();
            for (Map<Object, Object> paper : papersInfo)
            {
                String selfInfo = (String)paper.get("selfInfo");
                String parse = paperParse.parse(selfInfo);
                paper.put("selfInfo",parse);
                afterParse.add(paper);
            }
            return afterParse;
        }
        catch (Exception e)
        {
            System.out.println("???????????");
            System.out.println("纸条解密失败");
            System.out.println("???????????");
        }
        //解密失败
        return null;
    }

    //删除纸条
    @Override
    public MsgBackToFront delPaperById(Integer p_id)
    {
        //先删除照片
        Paper byId = this.getById(p_id);
        boolean ret = fileManager.delTargetFile(byId.getSelfImgs());
        if(!ret) return new MsgBackToFront(null,MsgBackToFront.DEL_FILE_ERROR);

        //先删除paper表
        boolean ret1 = this.removeById(p_id);
        //删除映射表
        //删除前，先判断映射表中是否有该纸条的数据
        Paperchosebywho pId = paperchosebywhoService.getOne(new QueryWrapper<Paperchosebywho>().eq("p_id", byId.getId()));
        if(pId==null)
            return ret1?new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS):new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);

        boolean ret2 = paperchosebywhoService.remove(new QueryWrapper<Paperchosebywho>().eq("p_id", byId.getId()));
        return (ret1&&ret2)?new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS):new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
    }
}
